import 'package:flutter/material.dart';
import 'package:flutter_stort/model/address_entity.dart';
import 'package:flutter_stort/utils/check_data_utils.dart';
import 'package:flutter_stort/utils/dialog_utils.dart';
import 'package:flutter_stort/utils/event_bus.dart';
import 'package:flutter_stort/utils/jump_util.dart';
import 'package:flutter_stort/utils/toast_utils.dart';

class EditAddressPage extends StatefulWidget {
  AddressEntity address;

  EditAddressPage({this.address});

  @override
  _EditAddressPageState createState() => _EditAddressPageState();
}

class _EditAddressPageState extends State<EditAddressPage> {
  GlobalKey<FormState> _saveAddressKey = GlobalKey();
  TextEditingController _nameController = TextEditingController();
  TextEditingController _mobileController = TextEditingController();
  TextEditingController _detailedAddressController = TextEditingController();
  String name;
  String mobile;
  String province;
  String city;
  String area;
  String address = "";
  String detailedAddress;
  String addressHint = "所在地区";
  bool _selectDetailed = true;

  @override
  void initState() {
    if (widget.address != null) {
      _nameController.text = widget.address.name;
      _mobileController.text = widget.address.mobile;
      _detailedAddressController.text = widget.address.detailedAddress;
      province = widget.address.province;
      city = widget.address.city;
      area = widget.address.area;
      address = "$province $city $area";
      _selectDetailed = widget.address.isDefault;
      setState(() {
        if (address == null && address.length <= 0) {
          addressHint = "所在地区";
        } else {
          addressHint = "";
        }
      });
    }
    super.initState();
  }

  void _saveAddress() {
    _saveAddressKey.currentState.save();
    if (checkData()) {
      AddressEntity addressEntity = AddressEntity(
        name: name,
        mobile: mobile,
        province: province,
        city: city,
        area: area,
        detailedAddress: detailedAddress,
        isDefault: _selectDetailed,
      );
      EventBusUtils.eventBus.fire(addressEntity);
      ToastUtil.showToast("地址添加成功");
      JumpPageUtils.clostPage(context);
    }
  }

  bool checkData() {
    if (name.isEmpty) {
      ToastUtil.showToast("请输入收件人姓名");
      return false;
    }
    if (mobile.isEmpty) {
      ToastUtil.showToast("请输入收件人联系方式");
      return false;
    }
    if (!CheckDataUtils.isPhone(mobile)) {
      ToastUtil.showToast("您输入的手机号码格式不正确");
      return false;
    }
    if (address.isEmpty) {
      ToastUtil.showToast("请选择收货地址");
      return false;
    }
    if (detailedAddress.isEmpty) {
      ToastUtil.showToast("请选择收货地址的详细信息");
      return false;
    }
    return true;
  }

  ///选择收货地址
  void _selectAddress() {
    DialogUtils.showSelectAddress(context, (value) {
      province = value.currentProvince.province;
      city = value.currentCity.city;
      area = value.currentDistrict.area;
      address = "$province $city $area";
      setState(() {
        if (address == null && address.length <= 0) {
          addressHint = "所在地区";
        } else {
          addressHint = "";
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.address == null ? "添加收货地址" : "编辑收货地址",
          style: TextStyle(
            color: Colors.white,
            fontSize: 20,
          ),
        ),
      ),
      body: Container(
        padding: EdgeInsets.only(left: 10, right: 10),
        child: Form(
          key: _saveAddressKey,
          child: ListView(
            children: [
              Container(
                height: 50,
                alignment: Alignment.center,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      width: 80,
                      child: Text(
                        "收货人",
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Expanded(
                      child: TextFormField(
                        controller: _nameController,
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: "收货人",
                          hintStyle: TextStyle(fontSize: 14),
                          suffixStyle: TextStyle(fontSize: 14),
                          labelStyle: TextStyle(fontSize: 14),
                        ),
                        onEditingComplete: () {},
                        onSaved: (value) {
                          name = value;
                        },
                      ),
                    ),
                  ],
                ),
              ),
              Divider(color: Colors.grey, height: 0.5),
              Container(
                height: 50,
                alignment: Alignment.center,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      width: 80,
                      child: Text(
                        "手机号码",
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Expanded(
                      child: TextFormField(
                        controller: _mobileController,
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: "手机号码",
                          hintStyle: TextStyle(fontSize: 14),
                          suffixStyle: TextStyle(fontSize: 14),
                          labelStyle: TextStyle(fontSize: 14),
                        ),
                        onEditingComplete: () {},
                        onSaved: (value) {
                          mobile = value;
                        },
                      ),
                    ),
                  ],
                ),
              ),
              Divider(color: Colors.grey, height: 0.5),
              Container(
                height: 50,
                alignment: Alignment.center,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      width: 80,
                      child: Text(
                        "所在地区",
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Expanded(
                      child: InkWell(
                        onTap: _selectAddress,
                        child: Container(
                          height: 50,
                          child: Row(
                            children: [
                              Expanded(
                                flex: 1,
                                child: Text.rich(
                                  TextSpan(
                                    text: addressHint,
                                    style: TextStyle(
                                      color: Colors.grey,
                                      fontSize: 16,
                                    ),
                                    children: [
                                      TextSpan(
                                        text: address,
                                        style: TextStyle(
                                          color: Colors.black,
                                          fontSize: 16,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                              Icon(
                                Icons.navigate_next,
                                color: Colors.grey,
                                size: 26,
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Divider(color: Colors.grey, height: 0.5),
              Container(
                padding: EdgeInsets.only(top: 10, bottom: 10),
                alignment: Alignment.center,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      width: 80,
                      height: 50,
                      alignment: Alignment.centerLeft,
                      child: Text(
                        "详细地址",
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Expanded(
                      child: TextFormField(
                        maxLines: 4,
                        minLines: 2,
                        controller: _detailedAddressController,
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: "详细地址：如街道、门派号、小区、楼栋号、单元室等",
                          hintMaxLines: 2,
                          hintStyle: TextStyle(fontSize: 16),
                        ),
                        onEditingComplete: () {},
                        onSaved: (value) {
                          this.detailedAddress = value;
                        },
                      ),
                    ),
                  ],
                ),
              ),
              Divider(color: Colors.grey, height: 0.5),
              Container(
                height: 50,
                child: Row(
                  children: [
                    Expanded(
                      flex: 1,
                      child: Container(
                        child: Text(
                          "设为默认地址",
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 16,
                          ),
                        ),
                      ),
                    ),
                    Checkbox(
                      value: _selectDetailed,
                      onChanged: (value) {
                        setState(() {
                          _selectDetailed = value;
                        });
                      },
                    ),
                  ],
                ),
              ),
              SizedBox(height: 50),
              Container(
                width: 300,
                height: 50,
                child: RaisedButton(
                  onPressed: _saveAddress,
                  color: Colors.orange,
                  shape: StadiumBorder(),
                  child: Text(
                    "保存",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 18,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
